package com.zdkj.admin.controller;


import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zdkj.admin.api.entity.SysLog;
import com.zdkj.admin.api.vo.PreLogVo;
import com.zdkj.admin.service.SysLogService;
import com.zdkj.common.core.util.R;
import com.zdkj.common.security.annotation.Inner;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.time.ZoneOffset;
import java.util.Collection;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 日志表 前端控制器
 * </p>
 *
 * @author cui_com
 * @since 2017-11-20
 */
@RestController
@AllArgsConstructor
@RequestMapping("/log")
@Api(value = "log", tags = "日志管理模块")
public class SysLogController {
    private final SysLogService sysLogService;

    /**
     * 简单分页查询
     *
     * @param page   分页对象
     * @param sysLog 系统日志
     * @return
     */
    @GetMapping("/page")
    @PreAuthorize("@pms.hasPermission('sys_log_query')")
    public R getLogPage(Page page, SysLog sysLog) {
        List<String> dateCondition = sysLog.getDates1();
        sysLog.setFiledFlag("1");
        QueryWrapper queryWrapper = Wrappers.query(sysLog);
        if (ObjectUtil.isNotEmpty(dateCondition)) {
            queryWrapper.between("create_time", dateCondition.get(0), dateCondition.get(1));
        }
        return R.ok(sysLogService.page(page, queryWrapper));
    }

    /**
     * 查询归档日志
     *
     * @param page
     * @param sysLog
     * @return
     */
    @GetMapping("/page/filedLogs")
    @PreAuthorize("@pms.hasPermission('sys_field_log_query')")
    public R getFiledLogPage(Page page, SysLog sysLog) {
        List<String> dateCondition1 = sysLog.getDates1();
        List<String> dateCondition2 = sysLog.getDates2();
        sysLog.setFiledFlag("0");
        LambdaQueryWrapper<SysLog> queryWrapper = Wrappers.<SysLog>lambdaQuery(sysLog);
        if (ObjectUtil.isNotEmpty(dateCondition1)) {
            queryWrapper.between(SysLog::getCreateTime, dateCondition1.get(0), dateCondition1.get(1));
        }
        if (ObjectUtil.isNotEmpty(dateCondition2)) {
            queryWrapper.between(SysLog::getFiledTime, dateCondition2.get(0), dateCondition2.get(1));
        }
        return R.ok(sysLogService.page(page, Wrappers.query(sysLog)));
    }

    /**
     * 归档日志
     *
     * @param logIds
     * @return
     */
    @PostMapping("/filedLogs")
    @PreAuthorize("@pms.hasPermission('sys_field_log')")
    public R filedLogs(@RequestBody List<Integer> logIds) {
        Collection<SysLog> sysLogList = sysLogService.listByIds(logIds);
        Date now = new Date();
        for (SysLog sysLog : sysLogList) {
            sysLog.setFiledFlag("0");
            sysLog.setFiledTime(now.toInstant().atOffset(ZoneOffset.of("+8")).toLocalDateTime());
        }
        return R.ok(sysLogService.updateBatchById(sysLogList));
    }

    /**
     * 删除归档日志
     *
     * @param logIds
     * @return
     */
    @DeleteMapping("/filedLogs")
    @PreAuthorize("@pms.hasPermission('sys_filed_log_del')")
    public R deleteFiledLogs(@RequestBody List<Integer> logIds) {
        Collection<SysLog> sysLogList = sysLogService.listByIds(logIds);
        for (SysLog sysLog : sysLogList) {
            sysLog.setFiledFlag("1");
            sysLog.setFiledTime(null);
        }
        return R.ok(sysLogService.updateBatchById(sysLogList));
    }


    /**
     * 删除日志
     *
     * @param id ID
     * @return success/false
     */
    @DeleteMapping("/{id}")
    @PreAuthorize("@pms.hasPermission('sys_log_del')")
    public R removeById(@PathVariable Long id) {
        return R.ok(sysLogService.removeById(id));
    }

    /**
     * 插入日志
     *
     * @param sysLog 日志实体
     * @return success/false
     */
    @Inner
    @PostMapping("/save")
    public R save(@Valid @RequestBody SysLog sysLog) {
        return R.ok(sysLogService.save(sysLog));
    }

    /**
     * 批量插入前端异常日志
     *
     * @param preLogVoList 日志实体
     * @return success/false
     */
    @PostMapping("/logs")
    public R saveBatchLogs(@RequestBody List<PreLogVo> preLogVoList) {
        return R.ok(sysLogService.saveBatchLogs(preLogVoList));
    }

    /**
     * 设置日志过期时间
     *
     * @param timeSecond
     * @return
     */
    @PostMapping("/set/expire/{timeSecond}")
    @PreAuthorize("@pms.hasPermission('sys_log_set_expire')")
    public R setLogExpireTime(@PathVariable Integer timeSecond) {
        sysLogService.setExpire(timeSecond);
        return R.ok();
    }

    /**
     * 获取日志过期时间
     *
     * @return
     */
    @GetMapping("/get/expire")
    public R getLogExpireTime() {
        return R.ok(sysLogService.getExpire());
    }


    /**
     * 归档过期日志
     *
     * @return
     */
    @ApiOperation(value = "归档过期日志", notes = "归档过期日志")
    @Inner
    @PostMapping("/file/expire")
    public R fileExpireTime() {
        return R.ok(sysLogService.fileExpireLog());
    }
}
